<template>
  <div class="ml-input " :class="{'ml-input--suffix':showSuffix}">
    <input
      :type="showPassword ? (passwordVisible ? 'text':'password'):type"
      class="ml-input_inner"
      :class="{ 'is-disabled': disabled }"
      :placeholder="placeholder"
      :name="name"
      :disabled="disabled"
      :value="value"
      @input="handleInput"
    />
    <span class="ml-input_suffix">
      <i class="ml-input_icon ml-icon-circle-close" v-if="clearable && value" @click="clear"></i>
      <i class="ml-input_icon ml-icon-view" 
      v-if="showPassword"
       @click="handlePassword"
       :class="{'ml-icon-view-active':passwordVisible}"
       ></i>
    </span>
  </div>
</template>

<script>
export default {
  name: "MlInput",
  data(){
      return{
          passwordVisible:false
      }
  },
  props: {
    placeholder: {
      type: String,
      default: "",
    },
    type: {
      type: String,
      default: "text",
    },
    name: {
      type: String,
      default: "",
    },
    disabled: {
      type: Boolean,
      default: false,
    },
    value: {
      type: String,
      default: "",
    },
    clearable: {
      type: Boolean,
      default: false,
    },
    showPassword: {
      type: Boolean,
      default: false,
    },
  },
  computed:{
      showSuffix(){
          return this.clearable || this.showPassword
      }
  },
  methods: {
    handleInput(e) {
      this.$emit("input", e.target.value);
    },
    clear(){
        this.$emit("input", ''); 
    },
    handlePassword(){
        this.passwordVisible = !this.passwordVisible;
    }
  },
};
</script>

<style lang="scss" scoped>
.ml-input {
  width: 100%;
  position: relative;
  font-size: 14px;
  display: inline-block;
  .ml-input_inner {
    -webkit-appearance: none;
    background-color: #fff;
    background-image: none;
    border: 1px solid #dcdfe6;
    box-sizing: border-box;
    color: #606266;
    display: inline-block;
    font-size: inherit;
    height: 40px;
    line-height: 40px;
    outline: none;
    padding: 0 15px;
    transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
    width: 100%;

    &:focus {
      outline: none;
      border-color: #409eff;
    }
    &.is-disabled {
      background-color: #f5f7fa;
      border-color: #e4e7ed;
      color: #c0c4cc;
      cursor: not-allowed;
    }
  }
}
.ml-input--suffix{
    .ml-input_inner{
        padding-right: 30px;
    }
    .ml-input_suffix{
        position: absolute;
        height: 100%;
        right: 10px;
        top: 0;
        line-height: 40px;
        text-align: center;
        color: #c0c4cc;
        transition: all .3s;
        z-index: 900;
        i{
            color: #c0c4cc;
            font-size: 14px;
            cursor: pointer;
            transition: color .2s cubic-bezier(.645,.045,.355,1);
        }
          .ml-icon-view-active{
        color: blue;
    }
    }
  
}
</style>